package com.shrct.businessprocess.customerservice.impl;

import com.shrct.businessprocess.customerservice.RCFaultReport;
import com.shrct.businessprocess.customerservice.RCFaultReportEventType;
import com.shrct.businessprocess.customerservice.RCFaultWorkItem;
import com.shrct.businessprocess.customerservice.RCFaultWorkItemReport;
import com.shrct.businessprocess.customerservice.RCFaultWorkItemStatus;
import com.shrct.businessprocess.customerservice.impl.RCFaultReportImpl;
import com.shrct.businessprocess.customerservice.impl.RCFaultWorkItemReportImpl;
import com.shrct.businessprocess.customerservice.proxy.RCFaultWorkItemProxy;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCFAULTWORKITEM_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcfaultworkitem")
public class RCFaultWorkItemImpl extends BaseModelObject implements RCFaultWorkItem {
  public RCFaultWorkItemImpl() {
    super();
  }
  
  public RCFaultWorkItemImpl(final RCFaultReport faultReport) {
    super();
    this.faultReport = faultReport;
  }
  
  private RCFaultWorkItemStatus Status;
  
  public RCFaultWorkItemStatus getStatus() {
    return this.Status;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    Status=RCFaultWorkItemStatus.created;
  }
  
  public void start() {
    if(Status==null){
    	_start();
    	getModelObjectLogService().createLog(this,"Status","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCFaultWorkItemEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void _accept() {
    
    this.faultReport.createEvent("\u5DE5\u7A0B\u5E08\u63A5\u53D7\u4EFB\u52A1", RCFaultReportEventType.engineerAccept);
    Status=RCFaultWorkItemStatus.accepted;
  }
  
  public void accept() {
    if(Status==RCFaultWorkItemStatus.created){
    	_accept();
    	getModelObjectLogService().createLog(this,"Status","accept");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCFaultWorkItemEventsManager.class).fireAccept(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _finish() {
    
    this.faultReport.createEvent("\u5DE5\u7A0B\u5E08\u5B8C\u6210\u4EFB\u52A1", RCFaultReportEventType.engineerReportFinish);
    Status=RCFaultWorkItemStatus.finished;
  }
  
  public void finish() {
    if(Status==RCFaultWorkItemStatus.accepted){
    	_finish();
    	getModelObjectLogService().createLog(this,"Status","finish");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCFaultWorkItemEventsManager.class).fireFinish(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  public Boolean isCreated() {
    return com.shrct.businessprocess.customerservice.RCFaultWorkItemStatus.created==Status;
  }
  
  public Boolean isAccepted() {
    return com.shrct.businessprocess.customerservice.RCFaultWorkItemStatus.accepted==Status;
  }
  
  public Boolean isFinished() {
    return com.shrct.businessprocess.customerservice.RCFaultWorkItemStatus.finished==Status;
  }
  
  @JoinColumn(name = "faultReport_id")
  @ManyToOne(targetEntity = RCFaultReportImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCFaultReport faultReport;
  
  public RCFaultReport getFaultReport() {
    return this.faultReport;
  }
  
  public RCFaultWorkItem setFaultReport(final RCFaultReport faultReport) {
    this.faultReport = faultReport;
    return this;			
    
  }
  
  @JoinColumn(name = "worker_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee worker;
  
  public RCEmployee getWorker() {
    return this.worker;
  }
  
  public RCFaultWorkItem setWorker(final RCEmployee worker) {
    this.worker = worker;
    return this;			
    
  }
  
  private Date assignDate;
  
  public Date getAssignDate() {
    return this.assignDate;
  }
  
  public RCFaultWorkItem setAssignDate(final Date assignDate) {
    this.assignDate = assignDate;
    return this;			
    
  }
  
  private Date assignTime;
  
  public Date getAssignTime() {
    return this.assignTime;
  }
  
  public RCFaultWorkItem setAssignTime(final Date assignTime) {
    this.assignTime = assignTime;
    return this;			
    
  }
  
  private String assignComment;
  
  public String getAssignComment() {
    return this.assignComment;
  }
  
  public RCFaultWorkItem setAssignComment(final String assignComment) {
    this.assignComment = assignComment;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCFaultWorkItemReportImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "workitem")
  private Collection<RCFaultWorkItemReport> reports = new java.util.ArrayList<com.shrct.businessprocess.customerservice.RCFaultWorkItemReport>();;
  
  public Collection<RCFaultWorkItemReport> getReports() {
    return this.reports;
  }
  
  public RCFaultWorkItemReport createAndAddtoReports() {
    getReports().size();
    com.shrct.businessprocess.customerservice.RCFaultWorkItemReport rCFaultWorkItemReport = new com.shrct.businessprocess.customerservice.impl.RCFaultWorkItemReportImpl(this);
    getObjectFactory().create(rCFaultWorkItemReport);
    getReports().add(rCFaultWorkItemReport);
    return rCFaultWorkItemReport;
    
  }
  
  public RCFaultWorkItemReport createAndAddtoReports(final Procedure1<RCFaultWorkItemReport> updater) {
    getReports().size();
    com.shrct.businessprocess.customerservice.RCFaultWorkItemReport rCFaultWorkItemReport = new com.shrct.businessprocess.customerservice.impl.RCFaultWorkItemReportImpl(this);
    getObjectFactory().create(rCFaultWorkItemReport);
    getReports().add(rCFaultWorkItemReport);
    return rCFaultWorkItemReport;
    
  }
  
  public RCFaultWorkItem removeAllReports() {
    for(RCFaultWorkItemReport obj : this.reports){
    	getObjectFactory().delete(obj);
    }
    this.reports.clear();
    return this;	
    
  }
  
  public RCFaultWorkItem removeFromReports(final RCFaultWorkItemReport rCFaultWorkItemReport) {
    this.reports.remove(rCFaultWorkItemReport);
    getObjectFactory().delete(rCFaultWorkItemReport);
    return this;
    
  }
  
  public RCFaultWorkItemReport createReport(final Procedure1<? super RCFaultWorkItemReport> postInit) {
    RCFaultWorkItemReport _createAndAddtoReports = this.createAndAddtoReports();
    final Procedure1<RCFaultWorkItemReport> _function = new Procedure1<RCFaultWorkItemReport>() {
        public void apply(final RCFaultWorkItemReport it) {
          postInit.apply(it);
          String _reportComment = it.getReportComment();
          RCFaultWorkItemImpl.this.faultReport.engineerReport(RCFaultWorkItemImpl.this, _reportComment);
        }
      };
    RCFaultWorkItemReport _doubleArrow = ObjectExtensions.<RCFaultWorkItemReport>operator_doubleArrow(_createAndAddtoReports, _function);
    return _doubleArrow;
  }
  
  public static RCFaultWorkItem create(final RCFaultReport faultReport, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCFaultWorkItem rCFaultWorkItem = new com.shrct.businessprocess.customerservice.impl.RCFaultWorkItemImpl(
    	faultReport
    );
    objectFactory.create(rCFaultWorkItem);
    return rCFaultWorkItem;			
    
  }
  
  public static RCFaultWorkItem createRCFaultWorkItemImpl(final RCFaultReport faultReport, final IObjectFactory objectFactory, final Procedure1<RCFaultWorkItem> init) {
    com.shrct.businessprocess.customerservice.RCFaultWorkItem rCFaultWorkItem = new com.shrct.businessprocess.customerservice.impl.RCFaultWorkItemImpl(
    	faultReport
    );
    objectFactory.create(rCFaultWorkItem);
    init.apply(rCFaultWorkItem);
    
    return rCFaultWorkItem;			
    
  }
  
  public RCFaultWorkItemProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCFaultWorkItemProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCFaultWorkItemProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setStatus(this.getStatus().name());
    return proxy;
    
  }
}
